* Replication Code: Did the Budapest Convention Deter Cybercrime? An Event Study Using a Panel of Countries
* Authors: Humza Khan and Pengfei Zhang
* Data: NATO

* Install programs
ssc install reghdfe
ssc install event_plot
ssc install did_imputation
ssc install csdid
ssc install drdid
ssc install ivreg2
ssc install avar
ssc install eventstudyinteract
ssc install did_multiplegt

* Import data 

** converts strings (characters/text) to numeric
destring, replace

*unique numeric identifier for each country
gen country_num = _n 

*Reshape the data from wide to long format
reshape long, i(country_num) j(year)

tsset country_num year //dataset as a time series dataset  

*Create the treatment variable, i.e., timeToTreat 
gen timeToTreat = year - Eventtime 
gen treatment = cond(year >= Eventtime,1,0)

* preserve the dataset for data analysis manipulation
preserve


*drop observations based on time window
drop if timeToTreat < -17 
drop if timeToTreat > 17

*bc event windows. 
forvalues l = 0/17 {  
		gen L`l'event = timeToTreat == `l'
	} 
forvalues l = 1/16 { 
		gen F`l'event = timeToTreat ==-`l' 
} 
drop F1event  

* check event window
tab timeToTreat

*Dynamic Regression  
reghdfe attack F*event L*event, absorb(country_num year) cluster(country_num)
estimates store ols  
						
********* full event plot ols
	event_plot ols, ///
	stub_lag(L#event) stub_lead(F#event) plottype(scatter) ciplottype(rcap) ///
	graph_opt(ytitle("Estimated Change in Cyberattacks Received") ylabel(, labsize(small)) xlabel(-16(1)17, labsize(small)) xtitle("Years Before and After Ratification") ///
		legend(off) /// Turn off the legend with this option
	/// the following lines replace default_look with something more elaborate
		xline(-1, lcolor(gs13) lpattern(dash)) yline(0, lcolor(gs13)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
	) ///
	lag_opt1(msymbol(O) color(black)) lag_ci_opt1(color(black))

******************************************
*** Graph: DiD Estimator Comparisons ***
******************************************

*** Borusyak et al. ***
did_imputation attack country_num year Eventtime, autosample minn(0) allhorizons pretrends(17) cluster(country_num) 
estimates store bjs  


*** Callaway and San'Anna *** SHOWS ALL ZERO VALUES
*ssc install csdid
*ssc install drdid
 gen gvar = cond(Eventtime==., 0, Eventtime)  
csdid attack, ivar(country_num) time(year) gvar(gvar) notyet event
estat event, estore(cs)  


*** Sun and Abraham ***
*ssc install ivreg2
*ssc install avar
gen lastcohort = Eventtime==r(max)
eventstudyinteract attack L*event F*event, vce(cluster country_num) absorb(country_num year) cohort(Eventtime) control_cohort(lastcohort)
matrix sa_b = e(b_iw)  
matrix sa_v = e(V_iw)

 
*** de Chaisemartin-D'Haultfoeuille ***
*ssc install did_multiplegt
did_multiplegt_old attack country_num year treatment, robust_dynamic dynamic(12) placebo(4) breps(100) cluster(country_num) 
matrix dcdh_b = e(estimates)  
matrix dcdh_v = e(variances)

 
*HTE graph
event_plot dcdh_b#dcdh_v cs sa_b#sa_v ols, ///
	stub_lag(Effect_# Tp# L#event L#event) stub_lead(Placebo_# Tm# F#event F#event) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.325(0.13)0.325) trimlead(4) trimlag(9) noautolegend ///
	graph_opt( ///
		xtitle("{bf: Years Before and After Ratification}",size(small)) ytitle("{bf: Estimated Change in Attacks}",size(small)) xlabel(-4(1)9, labsize(vsmall)) ylabel(-1(1)6, labsize(vsmall)) ///
		legend(order(2 "de Chaisemartin-D'Haultfoeuille" ///
				4 "Callaway-Sant'Anna" 6 "Sun-Abraham" 8 "OLS") rows(2) position(6) region(lcolor(black) lpattern(longdash)) size(small) placement(center) lalign(center)) ///
		xline(-1, lcolor(gs12) lpattern(dash)) yline(0, lpattern(dash) lcolor(gs12)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal) labsize(vsmall)) ///
	) ///
	lag_opt1(msymbol(O) color(orange)) lag_ci_opt1(color(orange)) ///
	lag_opt2(msymbol(Dh) color(blue)) lag_ci_opt2(color(blue)) ///
	lag_opt3(msymbol(Th) color(emerald)) lag_ci_opt3(color(emerald)) ///
	lag_opt4(msymbol(Sh) color(black)) lag_ci_opt4(color(black)) 
 

 



